package com.jhs.boot.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jhs.boot.common.result.PageResult;
import com.jhs.boot.common.result.Result;
import com.jhs.boot.system.model.form.SysRouteForm;
import com.jhs.boot.system.model.query.SysRouteQuery;
import com.jhs.boot.system.model.vo.SysRouteVO;
import com.jhs.boot.system.service.SysRouteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 节路由前端控制层
 *
 * @author zxy
 * @since 2025-11-22 10:18
 */
@Tag(name = "节路由接口")
@RestController
@RequestMapping("/api/v1/sys-route")
@RequiredArgsConstructor
public class SysRouteController  {

    private final SysRouteService sysRouteService;

    @Operation(summary = "路由分配权限")
    @PutMapping("/{routeId}/nodes")
    public Result<Void> assignMenusToRole(
            @PathVariable Long routeId,
            @RequestBody List<Long> nodeIds
    ) {
        sysRouteService.assignRoutesToNodes(routeId, nodeIds);
        return Result.success();
    }
    @Operation(summary = "获取路由的节点ID集合")
    @GetMapping("/{routeId}/nodesIds")
    public Result<List<Long>> getRouteNodesIds(
            @Parameter(description = "角色ID") @PathVariable Long routeId
    ) {
        List<Long> nodesIds = sysRouteService.getRouteNodesIds(routeId);
        return Result.success(nodesIds);
    }
    @Operation(summary = "节路由分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('ap:sys-route:query')")
    public PageResult<SysRouteVO> getSysRoutePage(SysRouteQuery queryParams ) {
        IPage<SysRouteVO> result = sysRouteService.getSysRoutePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增节路由")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('ap:sys-route:add')")
    public Result<Void> saveSysRoute(@RequestBody @Valid SysRouteForm formData ) {
        boolean result = sysRouteService.saveSysRoute(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取节路由表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('ap:sys-route:edit')")
    public Result<SysRouteForm> getSysRouteForm(
        @Parameter(description = "节路由ID") @PathVariable Long id
    ) {
        SysRouteForm formData = sysRouteService.getSysRouteFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改节路由")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('ap:sys-route:edit')")
    public Result<Void> updateSysRoute(
            @Parameter(description = "节路由ID") @PathVariable Long id,
            @RequestBody @Validated SysRouteForm formData
    ) {
        boolean result = sysRouteService.updateSysRoute(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除节路由")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('ap:sys-route:delete')")
    public Result<Void> deleteSysRoutes(
        @Parameter(description = "节路由ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = sysRouteService.deleteSysRoutes(ids);
        return Result.judge(result);
    }
}
